package com.fengxiaoxiaoxi.fengutils.utils.jdk;

import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ThreadTest1 {
    @Test
    void  test(){
        Thread t1 = new Thread() {
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(this.getName() + "....aaaaaaaaaaaaaaaaaaaaaaa");
                }
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(this.getName() + "....bb");
                }
            }
        };
        t1.setName("芙蓉姐姐");
        t2.setName("凤姐");

        t1.start();
        t2.start();

    }

    @Test
    void test2(){
        new Thread(new Runnable() {
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + "...aaaaaaaaaaaaaaaaaaaaa");
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                for(int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + "...bb");
                }
            }
        }).start();
        Thread.currentThread().setName("我是主线程");					//获取主函数线程的引用,并改名字
        System.out.println(Thread.currentThread().getName());		//获取主函数线程的引用,并获取名字
    }


    @Test
    void test3(){
        Thread t1 = new Thread() {
            public void run() {
                for(int i = 0; i < 50; i++) {
                    System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                for(int i = 0; i < 5; i++) {
                    System.out.println(getName() + "...bb");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t1.setDaemon(true);						//将t1设置为守护线程

        t1.start();
        t2.start();
    }

    @Test
    void test4 () {

        /**
         * @param args
         * 计时器
         * @throws InterruptedException
         */

        Timer t = new Timer();
        t.schedule(new MyTimerTask(), new Date(114, 9, 15, 10, 54, 20), 3000);

        while (true) {
            System.out.println(new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }



    }
    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("起床背英语单词");
        }


    }
}
